package com.seedwill.common.dozer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dozer.ConfigurableCustomConverter;

/**
 * DOZER转换器
 * 
 * 字符串转换
 * 
 * @author kdyi
 *
 */
public class StringAppendConverter implements ConfigurableCustomConverter {
	protected Log log = LogFactory.getLog(this.getClass());
	private String parameter;

	public String getParameter() {
		return parameter;
	}

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	public Object convert(Object existingDestinationFieldValue,
			Object sourceFieldValue, Class<?> destinationClass,
			Class<?> sourceClass) {
		return convert(existingDestinationFieldValue, sourceFieldValue,
				destinationClass, sourceClass, parameter);
	}

	public Object convert(Object existingDestinationFieldValue,
			Object sourceFieldValue, Class destinationClass, Class sourceClass,
			String appending) {
		Object result = null;
		if (sourceFieldValue != null) {
			try {
				if (sourceClass.isAssignableFrom(String.class)
						&& appending != null) {
					result = sourceFieldValue + appending;
				} else {
					result = sourceFieldValue;
				}
			} catch (RuntimeException e) {
				log.warn(
						"Data convert exception[existingDestinationFieldValue:"
								+ existingDestinationFieldValue+(existingDestinationFieldValue !=null ? "("+existingDestinationFieldValue.getClass()+")" : "") + "]"
								+ "[sourceFieldValue:" + sourceFieldValue +(sourceFieldValue !=null ? "("+sourceFieldValue.getClass()+")" : "")+ "]"
								+ "[destinationClass:" + destinationClass + "]"
								+ "[sourceClass:" + sourceClass + "]", e);
				throw e;
			}
		}
		return result;
	}
}
